package followme.yaohuile.com.followme.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import followme.yaohuile.com.followme.R;
import followme.yaohuile.com.followme.adapter.recommend.community.CommunityMianAdapter;
import followme.yaohuile.com.followme.bean.recommend.community.CommunityCounts;
import followme.yaohuile.com.followme.bean.recommend.community.CommunityData;
import followme.yaohuile.com.followme.bean.recommend.community.CommunityForum_list;
import followme.yaohuile.com.followme.bean.recommend.community.CommunityForum_listGroups;
import followme.yaohuile.com.followme.parsejson.recommend.community.CommunityDataParamers;
import followme.yaohuile.com.followme.utils.LogUtils;
import followme.yaohuile.com.followme.utils.community.CommunityInterfaceUrl;

public class CommunityFragment extends Fragment implements AdapterView.OnItemClickListener,View.OnClickListener{
	private View inflate;
	private List<CommunityForum_list> forum_lists;
	private String company;
	private String ask;
	private List<CommunityForum_listGroups> lastBrowse_item = new ArrayList<>();
	private GridView lastBrowse_gv;
	private CommunityMianAdapter lastBrawse_adapter;
	private LinearLayout lastBrawse_linearLayout;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 1:
					CommunityData data = (CommunityData) msg.obj;
					CommunityCounts communityCounts = data.getCounts();
					ask = communityCounts.getAsk() + "";
					company = communityCounts.getCompany() + "";
					LogUtils.LOG_D(getClass(), "ask--->" + ask + "company====" + company);
					forum_lists = data.getForum_list();
					LogUtils.LOG_D(getClass(), "forum_lists" + forum_lists.toString());
					break;
			}
			//初始化控件
			initView();
		}
	};

	public CommunityFragment() {
		// Required empty public constructor
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		inflate = inflater.inflate(R.layout.fragment_community, container, false);


//请求数据
		CommunityDataParamers.getCommunityList(CommunityInterfaceUrl.COMMUNITY_RUL, handler);


		return inflate;

	}

	private void initView() {
		RelativeLayout ask_rv = (RelativeLayout) inflate.findViewById(R.id.community_ask_RV);
		RelativeLayout campany_rv = (RelativeLayout) inflate.findViewById(R.id.commnunity_campany_RV);
		ask_rv.setOnClickListener(this);
		campany_rv.setOnClickListener(this);

		TextView ask_tv = (TextView) inflate.findViewById(R.id.community_ask_tv);
		TextView campany_tv = (TextView) inflate.findViewById(R.id.community_campany_tv);


//给控件设置解析到的值
		if (!TextUtils.isEmpty(ask) && ask != null) {
			ask_tv.setText(ask + "个问题得到解决");
		}
		if (!TextUtils.isEmpty(company) && company != null) {
			campany_tv.setText(company + "个网友在此结伴");
		}
//TODO 最近浏览
		lastBrowse_gv = (GridView) inflate.findViewById(R.id.community_lastBrowse_gv);
		lastBrawse_linearLayout = (LinearLayout) inflate.findViewById(R.id.lastBrawse_linearLayout);


//        lastBrowse_gv.setAdapter(lastBrawse_adapter);
		lastBrowse_gv.setOnItemClickListener(this);
//        GridViewListenser gridViewListenser = new GridViewListenser();


//给亚洲d的grideView设置设配器
		GridView community_main_gv = (GridView) inflate.findViewById(R.id.community_main_gv);
		final List<CommunityForum_listGroups> datas = forum_lists.get(0).getGroup();
		CommunityMianAdapter adapter = new CommunityMianAdapter(datas, getActivity());
		community_main_gv.setAdapter(adapter);
		community_main_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                lastBrowse_item.add(forum_lists.get(0).getGroup().get(position));
				AddLastBrawseListItem(0, position);

//                调到详情页面
//				Intent intent = new Intent(getActivity(), CommunityDetailActivity.class);
//				Bundle bundle = new Bundle();
//				CommunityForum_listGroups putdata = datas.get(position);
//				bundle.putSerializable("detail", putdata);
//				LogUtils.LOG_D(getClass(), "要传递的数据" + putdata.toString());
//				intent.putExtras(bundle);
//				startActivityForResult(intent, 88);

			}
		});

//初始化欧洲的gridView并设置适配器
		GridView Europe_girdView = (GridView) inflate.findViewById(R.id.community_Europe_gv);
		List<CommunityForum_listGroups> Europe_item = forum_lists.get(1).getGroup();
		CommunityMianAdapter Europe_Adapter = new CommunityMianAdapter(Europe_item, getActivity());
		Europe_girdView.setAdapter(Europe_Adapter);
//        Europe_girdView.setOnItemClickListener(gridViewListenser);
		Europe_girdView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                lastBrowse_item.add(forum_lists.get(1).getGroup().get(position));
//                AddLastBrawseListItem(1,position);
//                lastBrawse_adapter = new CommunityMianAdapter(lastBrowse_item,getActivity());
//                lastBrowse_gv.setAdapter(lastBrawse_adapter);
//                LogUtils.LOG_D(getClass(),"item=="+forum_lists.get(1).getGroup().get(position));
//                LogUtils.LOG_D(getClass(), "lastBrowse_item.size()==" + lastBrowse_item.size());
			}
		});

//初始化美洲的gridView并设置适配器
		GridView America_girdView = (GridView) inflate.findViewById(R.id.community_America_gv);
		List<CommunityForum_listGroups> America_item = forum_lists.get(2).getGroup();
		CommunityMianAdapter America_Adapter = new CommunityMianAdapter(America_item, getActivity());
		America_girdView.setAdapter(America_Adapter);
		America_girdView.setOnItemClickListener(this);


//初始化大洋洲的gridView并设置适配器
		GridView Oceania_girdView = (GridView) inflate.findViewById(R.id.community_Oceania_gv);
		List<CommunityForum_listGroups> Oceania_item = forum_lists.get(3).getGroup();
		CommunityMianAdapter Oceania_Adapter = new CommunityMianAdapter(Oceania_item, getActivity());
		Oceania_girdView.setAdapter(Oceania_Adapter);
		Oceania_girdView.setOnItemClickListener(this);

//初始化非洲的gridView并设置适配器
		GridView Africa_girdView = (GridView) inflate.findViewById(R.id.community_Africa_gv);
		List<CommunityForum_listGroups> Africa_item = forum_lists.get(4).getGroup();
		CommunityMianAdapter Africa_Adapter = new CommunityMianAdapter(Africa_item, getActivity());
		Africa_girdView.setAdapter(Africa_Adapter);
		Africa_girdView.setOnItemClickListener(this);


//初始化兴趣小组的gridView并设置适配器
		GridView interestGroup_girdView = (GridView) inflate.findViewById(R.id.community__interestGroup_gv);
		List<CommunityForum_listGroups> interestGroup_item = forum_lists.get(5).getGroup();
		CommunityMianAdapter interestGroup_Adapter = new CommunityMianAdapter(interestGroup_item, getActivity());
		interestGroup_girdView.setAdapter(interestGroup_Adapter);
		interestGroup_girdView.setOnItemClickListener(this);


//初始化线上线下的gridView并设置适配器
		GridView onAnddownLine_girdView = (GridView) inflate.findViewById(R.id.community__onAnddownLine_gv);
		List<CommunityForum_listGroups> onAnddownLine_item = forum_lists.get(6).getGroup();
		CommunityMianAdapter onAnddownLine_Adapter = new CommunityMianAdapter(onAnddownLine_item, getActivity());
		onAnddownLine_girdView.setAdapter(onAnddownLine_Adapter);
		onAnddownLine_girdView.setOnItemClickListener(this);

//初始化后院的gridView并设置适配器
		GridView backyard_girdView = (GridView) inflate.findViewById(R.id.community__backyard_gv);
		List<CommunityForum_listGroups> backyard_item = forum_lists.get(7).getGroup();
		CommunityMianAdapter backyard_Adapter = new CommunityMianAdapter(backyard_item, getActivity());
		backyard_girdView.setAdapter(backyard_Adapter);
		backyard_girdView.setOnItemClickListener(this);

	}

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//    }

	//实现问答，结伴页面的跳转事件
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
			case R.id.commnunity_campany_RV:
//				Intent campany_intent = new Intent();
//				campany_intent.setClass(getActivity(), CompanyActivity.class);
//				startActivityForResult(campany_intent,88);

				break;
			case R.id.community_ask_RV:
//				Intent ask_intent = new Intent();
//				ask_intent.setClass(getActivity(), AskAnswerActivity.class);
//				startActivityForResult(ask_intent,88);
				break;
		}

	}

	//gridView的监听事件
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


		switch (view.getId()) {
			case R.id.community_main_gv:

				break;
			case R.id.community_Europe_gv:
				lastBrowse_item.add(forum_lists.get(1).getGroup().get(position));
				AddLastBrawseListItem(1, position);
				break;
			case R.id.community_America_gv:
				lastBrowse_item.add(forum_lists.get(2).getGroup().get(position));
				AddLastBrawseListItem(2, position);
				break;
			case R.id.community_Oceania_gv:
				lastBrowse_item.add(forum_lists.get(3).getGroup().get(position));
				AddLastBrawseListItem(3, position);
				break;
			case R.id.community_Africa_gv:
				lastBrowse_item.add(forum_lists.get(4).getGroup().get(position));
				AddLastBrawseListItem(4, position);
				break;
			case R.id.community__interestGroup_gv:
				lastBrowse_item.add(forum_lists.get(5).getGroup().get(position));
				AddLastBrawseListItem(5, position);
				break;
			case R.id.community__onAnddownLine_gv:
				lastBrowse_item.add(forum_lists.get(6).getGroup().get(position));
				AddLastBrawseListItem(6, position);
				break;
			case R.id.community__backyard_gv:
				lastBrowse_item.add(forum_lists.get(7).getGroup().get(position));
				AddLastBrawseListItem(7, position);
				break;
		}

	}

	//    给最近浏览的list集合中添加数据
	public void AddLastBrawseListItem(int j, int position) {
		lastBrowse_item.add(forum_lists.get(j).getGroup().get(position));
		if (lastBrowse_item.size() != 0) {
			lastBrowse_gv.setVisibility(View.VISIBLE);
			lastBrawse_linearLayout.setVisibility(View.VISIBLE);
		}
		for (int i = 1; i < lastBrowse_item.size(); i++) {
			lastBrowse_item.set(i, lastBrowse_item.get(i - 1));
//            if (lastBrowse_item.contains(forum_lists.get(j).getGroup().get(position))){
//                lastBrowse_item.set(0,forum_lists.get(j).getGroup().get(position));
//                lastBrowse_item.remove(forum_lists.get(j).getGroup().get(position));
//            }
			if (lastBrowse_item.size() > 6) {
				lastBrowse_item.remove(6);
			}
		}
		lastBrowse_item.set(0, forum_lists.get(j).getGroup().get(position));
		lastBrawse_adapter = new CommunityMianAdapter(lastBrowse_item, getActivity());
		lastBrowse_gv.setAdapter(lastBrawse_adapter);
		LogUtils.LOG_D(getClass(), "item==" + forum_lists.get(j).getGroup().get(position));
		LogUtils.LOG_D(getClass(), "lastBrowse_item.size()==" + lastBrowse_item.size());

	}
}
